/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <windows.h>
#include "PlayerHandler.h"
#include "../../tools/PacketReader.h" 
#include "../../tools/PacketCreator.h" 
#include "../../server/PlayerInventory.h"
#include "../../client/PetCommandData.h"
#include "../../server/ObjectMoving.h"
#include "../../client/PetsData.h"
#include "../../client/PetData.h"
#include "../../server/Player.h"
#include "../../server/Pet.h"
#include "../../server/Map.h"
using namespace Tools;

void PlayerHandler::usePetHandle(PacketReader& packet) {
	// TODO: check if player got the skill 8 (for multi pets)
	packet.readInt();
	Item* item = player->getInventories()->getItemBySlot(CASH, packet.readShort());
	if(item == 0 || MAJOR_TYPE(item->getID()) != 500)
		return;
	Pet* pet = (Pet*)item;
	if(player->getPet(pet->getObjectID()) != 0) {
		player->removePet(pet);
	} else {
		player->addPet(pet);
	}
}

void PlayerHandler::movePetHandle(PacketReader& packet) {
	Pet* pet = player->getPet(packet.readInt());
	if(pet == 0)
		return;
	packet.readInt();
	Position pos;
	pos.x = packet.readShort();
	pos.y = packet.readShort();
	ObjectMoving moving = ObjectMoving(packet, pet);
	player->getMap()->send(PacketCreator().movePet(player->getID(), pet->getPetSlot(), moving, pos));
}

void PlayerHandler::petCommandHandle(PacketReader& packet) {
	Pet* pet = player->getPet(packet.readInt());
	if(pet == 0)
		return;
	packet.read(5);
	char commandid = packet.read();
	PetData* data = PetsData::getInstance()->getDataByID(pet->getItemID());
	if(data == 0)
		return;
	PetCommandData* command = data->getDataByID(commandid);
	if(command == 0)
		return;
	bool success = pet->getLastTime() + 45*1000/(pet->getLevel()/10+1) < (int)GetTickCount() && random(99) < command->getProb();
	if(success) { 
		pet->addCloseness(command->getCloseness(), player); 
		pet->setLastTime(GetTickCount());
	}
	player->send(PacketCreator().petCommandReplay(player->getID(), pet->getPetSlot(), commandid, success));
}

void PlayerHandler::petCommandTextHandle(PacketReader& packet) {
	Pet* pet = player->getPet(packet.readInt());
	if(pet == 0)
		return;
	packet.read(5);
	char act = packet.read();
	player->getMap()->send(PacketCreator().showPetText(player->getID(), pet->getPetSlot(), packet.readString(), act));
}